package com.ryskoo.basicdata.area.region.repository;

import com.alibaba.fastjson.JSONArray;
import com.ryskoo.basicdata.area.region.domain.RegionPO;
import com.ryskoo.common.constant.CacheConstant;
import com.ryskoo.framework.core.repository.BasicRedisImpl;
import com.ryskoo.framework.core.utils.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author qyc
 */
@Primary
@Repository
public class RegionRedisImpl extends BasicRedisImpl<RegionPO> implements IRegionRepository{
    @Autowired
    private RedisUtil redisClient;

    @Autowired
    private IRegionRepository regionRepository;

    /**
     * 区县缓存KEY
     */
    private static final String cacheKey = CacheConstant.BDS_AREA_REGION_KEY;
    /**
     * 区县集合缓存KEY
     */
    private static final String cacheListKey = CacheConstant.BDS_AREA_REGION_LIST_KEY;
    /**
     * 市下的区县集合缓存KEY
     */
    private static final String cacheRegionCityKey = CacheConstant.BDS_AREA_REGION_CITY_KEY;

    @PostConstruct
    public void init() {
        this.setRedisClient(redisClient);
        this.setDao(regionRepository);
    }

    public RegionRedisImpl() {
        super(cacheKey);
    }

	@Override
	public List<RegionPO> getAll() {
		//1.从缓存获取所有的地区-区县
		Object object = redisClient.getSession(cacheListKey);
		if (object != null) {
            List<RegionPO> cacheList = (List<RegionPO>)object;
            return cacheList;
        }
		//2.缓存中如果没有取到，则从数据库中获取
		List<RegionPO> dbList = super.getAll();
		
		if(!CollectionUtils.isEmpty(dbList)){
			//2.1 归类市、区县存放Map key=市编号，value=区县集合
			Map<String,List<RegionPO>> regionMap = new HashMap<String,List<RegionPO>>();
			for(RegionPO po : dbList){
				String key =po.getParentId();
				//判断当前key是否存在
				if(regionMap.containsKey(key)){
					List<RegionPO> tempList = regionMap.get(key);
					if(CollectionUtils.isEmpty(tempList)){
						tempList = new ArrayList<RegionPO>();
					}
					tempList.add(po);
				}else{
					List<RegionPO> tempList = new ArrayList<RegionPO>();
					tempList.add(po);
					regionMap.put(key, tempList);
				}
			}
			//2.2 把归类好的市、区县关系存放到缓存中
			if(!CollectionUtils.isEmpty(regionMap)){
				for(String key : regionMap.keySet()){
					List<RegionPO> tempList = regionMap.get(key);
					redisClient.setObject(cacheRegionCityKey + key, tempList);
				}
			}
		}
		//3.将结果放入缓存
        redisClient.setExpireSession(cacheListKey, dbList, null);

		return dbList;
	}

	@Override
	public List<RegionPO> getRegionsByCityId(String cityId) {
		//1.根据key从缓存获取所有的地区-区县
		/*Object object = redisClient.getObject(cacheRegionCityKey+cityId,List.class);
		if (object != null) {
            List<RegionPO> cacheList = (List<RegionPO>)object;
            return cacheList;
        }*/
		String jsonStr = redisClient.getString(cacheRegionCityKey+cityId);
		if(!StringUtils.isEmpty(jsonStr)){
			List<RegionPO> cacheList = JSONArray.parseArray(jsonStr,RegionPO.class);
			return cacheList;
		}
		//2.缓存中如果没有取到，则从数据库中获取
		List<RegionPO> dbList = regionRepository.getRegionsByCityId(cityId);
		//3.将结果放入缓存
        //redisClient.setObject(cacheRegionCityKey+cityId, dbList);
		redisClient.setObject(cacheRegionCityKey+cityId, JSONArray.toJSONString(dbList));
        
		return dbList;
	}

	@Override
	public RegionPO getByEntityNo(String entityNo) {
		return regionRepository.getByEntityNo(entityNo);
	}
}
